<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\store\controller;

use app\api\model\user\SendMessage as SendMessageModel;
use think\facade\Queue;
use think\response\Json;
use app\store\model\Goods as GoodsModel;
use app\common\exception\BaseException;

/**
 * 商品管理控制器
 * Class Goods
 * @package app\store\controller
 */
class Goods extends Controller
{
    /**
     * 商品列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function list(): Json
    {
        // 获取列表记录
        $model = new GoodsModel;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 根据商品ID集获取列表记录
     * @param array $goodsIds
     * @return Json
     */
    public function listByIds(array $goodsIds): Json
    {
        // 获取列表记录
        $model = new GoodsModel;
        $list = $model->getListByIds($goodsIds);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 商品详情
     * @param int $goodsId
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \cores\exception\BaseException
     */
    public function detail(int $goodsId): Json
    {
        // 获取商品详情
        $model = new GoodsModel;
        $goodsInfo = $model->getDetail($goodsId);
        return $this->renderSuccess(compact('goodsInfo'));
    }

    /**
     * 添加商品
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \cores\exception\BaseException
     */
    public function add(): Json
    {
        $model = new GoodsModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑商品
     * @param int $goodsId
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \cores\exception\BaseException
     */
    public function edit(int $goodsId): Json
    {
        // 商品详情
        $model = GoodsModel::detail($goodsId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 修改商品状态(上下架)
     * @param array $goodsIds 商品id集
     * @param bool $state 为true表示上架
     * @return Json
     */
    public function state(array $goodsIds, bool $state): Json
    {
        $model = new GoodsModel;
        if (!$model->setStatus($goodsIds, $state)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 删除商品
     * @param array $goodsIds
     * @return Json
     */
    public function delete(array $goodsIds): Json
    {
        $model = new GoodsModel;
        if (!$model->setDelete($goodsIds)) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 商品返利类型
     * @return array|mixed
     */
    public function rebate()
    {
        $model = new GoodsModel;
        $list = $model::goodsRebateType();
        return $this->renderSuccess(compact('list'));
    }
    /**
     * 订单商品列表导出
     * @param int $supplierId
     * @return array
     */
    public function goods_export()
    {
        // 订单列表
        $model = new GoodsModel;
        $list = $model->goodsListExport($this->request->param());
        if ($list) {
            $path = parse_url($list, PHP_URL_PATH);
            $filename = basename($path);
            return $this->renderSuccess(array('download_url'=>$list,'download_name'=>$filename));
        }
        return $this->renderError($model->getError() ?: '导出失败');
    }

    /**
     * 新品上线微信订阅通知
     * @param int $goodsId
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \cores\exception\BaseException
     */
    public function send_dy(int $goodsId,$reason): Json
    {
        if (empty($goodsId) || empty($reason)){
            return $this->renderError( '参数错误');
        }
        $goodsInfo = GoodsModel::detail($goodsId);
        if(empty($goodsInfo)){
            return $this->renderError( '商品不存在');
        }
        if($goodsInfo['good_type'] == 'virtual_good'){
            return $this->renderError( '虚拟商品不支持');
        }
        if($goodsInfo['status'] == 20){
            return $this->renderError( '下线商品不支持');
        }
        //发送前清除发送结果
        $SendMessageModel = new SendMessageModel();
        $update['send_result'] = '';
        $update['send_status'] = 0;
        $SendMessageModel->where(['type' => 'new_arrival'])->update($update);
        // 队列名称
        $queueName = 'wx_message';
        // 队列的数据
        $data = [
            'goodsId'=> $goodsId,
            'reason' => $reason,
            'type'   => 'new_arrival'
        ];
        // 队列处理类
        $queueClass = \app\queue\WxMessage::class;
        if(Queue::push($queueClass, $data, $queueName)){
            return $this->renderSuccess('发送成功');
        }
        return $this->renderError( '发送失败');
    }
}
